<?php

namespace frontend\modules\Player\controllers;

use common\helpers\VoteHelper;
use common\models\c2\entity\ActivityModel;
use common\models\c2\entity\ActivityPlayerModel;
use common\models\c2\entity\ActivityPlayerVoteRecordModel;
use common\models\c2\search\ActivityPlayerSearch;
use common\models\c2\search\ActivityPlayerVoteRecordSearch;
use common\models\c2\statics\ActivityPlayerState;
use common\models\c2\statics\VoteType;
use common\models\c2\statics\Whether;
use common\rest\statics\ResponseDatum;
use frontend\components\actions\PosterAction;
use frontend\components\behaviors\WechatAuthBehavior;
use frontend\controllers\ActivityController;
use frontend\models\c2\entity\VoteRecordModel;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
 * Default controller for the `player` module
 */
class DefaultController extends ActivityController
{

    public $enableCsrfValidation = false;

    // public $layout = '/main-empty';
    // return $this->render('index', [
    //     'playerModel' => $playerModel,
    //     'model' => $model,
    // ]);

    public function actions()
    {
        return ArrayHelper::merge(parent::actions(), [
            'poster' => PosterAction::className(),
            'error' => [
                'layout' => 'main-empty',
                'class' => 'yii\web\ErrorAction',
            ],
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            // 'wechatFilter' => [
            //     'class' => WechatAuthBehavior::className()
            // ]
        ];
    }

    /**
     * Renders the index view for the module
     * @param null $c
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionIndex($c = null)
    {
        $model = ActivityPlayerModel::findOne(['player_code' => $c]);
        $model->updateCounters(['view_number' => 1]);

        if (is_null($model) || $model->state == ActivityPlayerState::STATE_NOT_CHECK) {
            throw new NotFoundHttpException(Yii::t('app.c2', 'Player disable.'));
        }

        $activity = $model->activity;
        if (is_null($activity) || $activity->is_released == Whether::TYPE_NO) {
            throw new NotFoundHttpException(Yii::t('app.c2', 'Activity not exist.'));
        }
        $this->activity = $activity;

        return $this->render('index', ['model' => $model]);
    }

    public function actionPlayers($vasc)
    {
        $this->layout = '/main-empty';
        $activityModel = ActivityModel::findOne(['seo_code' => $vasc]);
        $searchModel = new ActivityPlayerSearch();
        $searchModel->activity_id = $activityModel->id;
        $searchModel->state = ActivityPlayerState::STATE_CHECKED;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('players', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'activityModel' => $activityModel,
        ]);
    }

    public function actionFreeVote()
    {

        $id = Yii::$app->request->get('id');
        $playerModel = ActivityPlayerModel::findOne($id);
        $user_id = Yii::$app->user->id;

        $activityModel = $playerModel->activity;
        $this->activity = $activityModel;

        $message = VoteHelper::checkLimit($activityModel, $playerModel->id, $user_id);
        if ($message == "ycan") {
            $model = new VoteRecordModel();
            $model->loadDefaultValues();
            $model->setAttributes([
                'type' => VoteType::TYPE_FREE,
                'user_id' => $user_id,
                'activity_player_id' => $playerModel->id,
                'vote_number' => 1,
                'remote_ip' => Yii::$app->request->userIP,
            ]);
            if ($model->save()) {
                $message = Yii::t('app.c2', 'Vote Success');
            }
        }

        return $this->renderAjax('content', [
            'playerModel' => $playerModel,
            'activityModel' => $activityModel,
            'message' => $message,
        ]);
    }

    public function actionVote()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        if (!Yii::$app->request->isPost) {
            return '|';
        }
        $params = Yii::$app->request->post();
        $player = ActivityPlayerModel::findOne($params['id']);
        $message = VoteHelper::checkLimit($player, Yii::$app->user->id);
        if ($message != "ycan") {
            return ResponseDatum::getErrorDatum(['message' => $message], false);
        }
        $model = new \api\models\c2\entity\VoteRecordModel();
        $attributes = [
            'user_id' => Yii::$app->user->id,
            'type' => VoteType::TYPE_FREE,
            'activity_player_id' => $player->id,
            'vote_number' => 1,
            'remote' => Yii::$app->request->remoteIP,
        ];
        $model->setAttributes($attributes);
        if ($model->validate() && $model->save()) {
            return ResponseDatum::getSuccessDatum(['message' => Yii::t('app.c2', 'Vote Success')], $player->total_vote_number);
        } else {
            return ResponseDatum::getErrorDatum([], $model->errors);
        }
    }

}
